﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class AGVBase : MonoBehaviour
{
    private Vector3 gTargetPosition;
    private int gCurrentIndex=0;//
    private int gTarget1Index = 0;//
    private int gTarget2Index = 0;//
    private bool gIsStop=true;
    private bool gIsFree=true;
    private GameObject gMaterail;
    private float gMoveSpeed = 100;
    List<Vector3> gCarTaskList;
    List<int> gCarTaskIndexList;


    // Use this for initialization
    public void Start()
    {
        gTargetPosition = this.transform.position;//初始化目标点，为物体本身目标
        gCarTaskList = new List<Vector3>();
        gCarTaskIndexList = new List<int>();
    }
	
	// Update is called once per frame
	void Update () {
		if(!gIsStop)//如果没有停止，则表示在移动
        {
            this.transform.LookAt(gTargetPosition);//方向旋转
            this.transform.position=Vector3.MoveTowards(this.transform.position, gTargetPosition, Time.deltaTime * gMoveSpeed);//小车移动
            if (Vector3.Distance(transform.position, gTargetPosition) <= 0.5f)//判定是否已经到达目的地
            {
                if (gCarTaskList.Count != 0)//小车继续往下一个点移动
                {
                    gTargetPosition = gCarTaskList[0];
                    gCurrentIndex = gCarTaskIndexList[0];
                    gCarTaskList.RemoveAt(0);
                    gCarTaskIndexList.RemoveAt(0);
                }
                else//已经到达目标点
                {
                    gIsStop = true;//小车停止
                }
            }
        }
	}

    /// <summary>
    /// 更改目标点，驱动小车运动
    /// 通过改变停止属性，另小车运动
    /// </summary>
    /// <param name="iTargetPosition"></param>
    public void SetCarTrack(List<Vector3> iCarTrackList,List <int> iCarTrackIndexList)
    {
        gCarTaskList.Clear();
        gCarTaskIndexList.Clear();
        if (iCarTrackList.Count > 0)
        {
            gCarTaskList = iCarTrackList;
            gCarTaskIndexList = iCarTrackIndexList;
            gTargetPosition = gCarTaskList[0];
            gCurrentIndex = gCarTaskIndexList[0];
            gCarTaskIndexList.RemoveAt(0);
            gCarTaskList.RemoveAt(0);
            gIsStop = false;//令小车移动
        }
        else
        {
            gIsStop = true;//小车停止
        }
    }

    public void SetTargetAndAcceptIndex(int iCurrentIndex,int iTarget1Index,int iTarget2Index)
    {
        gCurrentIndex = iCurrentIndex;
        gTarget1Index= iTarget1Index;
        gTarget2Index = iTarget2Index;

    }

    /// <summary>
    /// 判断是否到达目的地
    /// </summary>
    public bool IsStop
    {
        get
        {
            return gIsStop;
        }

    }

    public bool IsFree
    {
        set { gIsFree = value; }
        get { return gIsFree; }
    }

    /// <summary>
    /// 获得任务状态
    /// </summary>
    /// <returns></returns>
    public int GetCurrentIndex()
    {
        return gCurrentIndex;
    }

    public int GetTarget1Index()
    {
        return gTarget1Index;
    }

    public int GetTarget2Index()
    {
        return gTarget2Index;
    }

    public void AddMaterial(GameObject iMaterail)
    {
        //小车获得材料
        gMaterail = iMaterail;
        gMaterail.SetActive(true);
        //定位物体在小车上的位置
        Vector3 pNewPosition = new Vector3(this.transform.position.x, 35f, this.transform.position.z);
        gMaterail.transform.position = pNewPosition;
        gMaterail.transform.parent = this.transform;
    }

    /// <summary>
    /// 用来判断是否有货物
    /// </summary>
    /// <returns></returns>
    public GameObject ReturnMaterial()
    {
        if (gMaterail != null)
        {
            return gMaterail;
        }
        return null;
    }
     
}
